package com.example.demo.foundation;

/**
 * @author yunda
 * @date 2021/1/16 16:30
 */
public class ThreadTest {

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();

        Thread thread1 = new Thread(() -> {
            try {
                synchronized (threadTest) {
                    System.out.println("to be waiting");
                    // 释放锁，进入monitor等待队列  注意：sleep会阻塞当前线程，进入TIME_WAITING状态，但是不会释放锁资源，超时自动返回
                    threadTest.wait();
                    System.out.println("return from wait after be notified");
                }
            } catch (InterruptedException e) {
                // 响应中断，设置中断标识
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().isInterrupted());
            System.out.println("thread1 is executing");
        });
        thread1.start();

        /*Thread thread2 = new Thread(() -> {
            System.out.println("notify");
            synchronized (threadTest) {
                threadTest.notify();
            }
        });
        thread2.start();*/


        Thread thread3 = new Thread(() -> {
            synchronized (threadTest) {
                System.out.println("notify");
                threadTest.notify();
            }
        });
        thread3.start();

    }

}
